home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: 73700.776@compuserve.com (Walter C. Riley)
- Newsgroups: comp.lang.c++
- Subject: Re: overiding c++ stream classes anyone?
- Date: Tue, 23 Jan 1996 21:37:41 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4e3kh5$90r@dub-news-svc-6.compuserve.com>
- References: <4cqbja$62n@earth.njcc.com> <4e1d5f$5en@news02.comp.pge.com>
- NNTP-Posting-Host: ad45-166.compuserve.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- psk3@pge.com (Phillip Knight) wrote:
-
- >I have pulled the last hair out of my head and still have not solved this
- >problem:
-
- > class X : public ostream {
- > public:
- > ...
- > X& operator<<(X& (*f)(X&)) { return (*f)(*this); }
- > };
-
-
- > X& newl(X& os) {
- > cerr << "this never gets printed" << endl;
- > }
-
- > main() {
- > X foo;
- > foo << "test one";
- > foo << endl;
-
- > foo << "test two" << endl;
- > }
-
- >so the problem is simple: how do i get my manipulator newl to be called? This
- >program as stands does not compile, because inclusion of the operator '<<'
- >(used in order to execute the manipulator) causes class X to no longer
- >recognize the base class operators (it only recognizes X's operator <<).
- >taking the operator out of the class and into the code as a function doesn't
- >solve the problem either as newl never gets called.
-
- >so anyone know a simple way of overloading a manipulator? (all this when all
- >i really need is a virtual flush...)
-
- >thanks in advance...
- >phil knight
- >psk3@pge.com
-
- Phil,
- Normally, you won't overload ostream just to create a new manipulator.
- Try something like this:
-
- #include <strstrea.h>
- #include <iomanip.h>
-
- ostream& MyNewLine(ostream& os) {
- return os << endl;
- }
-
- void main() {
- char buf[1000];
- ostrstream os(buf, sizeof(buf));
- os <<"This is a test" << MyNewLine << "to see what happens" << ends;
- cout << buf;
- }
-
- Good luck,
- Walt
-
-
-